<?php
/**
 * In Array Rule
 * Validates that a value is in the specified array. Can optionally check that
 * the value is the same case (default) and that the value is the same type.
 *
 * @package     Q-Validate
 * @subpackage  Rules
 * @author      Luke Visinoni <luke.visinoni@gmail.com>
 * @copyright   (c) 2010 Luke Visinoni <luke.visinoni@gmail.com>
 * @version     $Id: InArray.php 37 2010-12-23 02:38:44Z luke.visinoni $
 */
class Q_Rule_InArray extends Q_Rule_Abstract {

    /**
     * The parameters that were passed to this rule
     */
    protected $_params = array(
        'array' => array(),
        'caseSensitive' => true,
        'strict' => false,
    );
    
    /**
     * @todo Add a "case_insensitive" param
     */
    public function isValid($value) {
    
        $array = (array) $this->getParam('array');
        if ($this->getParam('caseSensitive')) {
            // loop through each item
            foreach ($array as $check) {
                // if the value is in the array
                if ($check == $value) {
                    // check that the case is the same
                    if (strcasecmp($value, $check) == 0) {
                        // if it is the same, and we are checking type as well,
                        // check type
                        if ($this->getParam('strict')) {
                            return ($check === $value);
                        } else {
                            // otherwise return true
                            return true;
                        }
                    }
                }
            }
        } else {
            return (in_array($value, $array, $this->getParam('strict')));
        }
        return false;
    
    }

}
